

#include "./motor/motor.h"
#include "string.h"
#include "stdio.h"


int status =1;





void motor0_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;  
    GPIO_InitStruct.Pull  = GPIO_PULLUP; 
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; 
	
		__HAL_RCC_GPIOD_CLK_ENABLE();
	__HAL_RCC_GPIOE_CLK_ENABLE();
	__HAL_RCC_GPIOF_CLK_ENABLE();
	__HAL_RCC_GPIOA_CLK_ENABLE();
	
	
	GPIO_InitStruct.Pin = PIN_CMD;
    HAL_GPIO_Init(GPIO_CMD, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = PIN_A;
    HAL_GPIO_Init(GPIO_A, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = PIN_B;
    HAL_GPIO_Init(GPIO_B, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = PIN_C;
    HAL_GPIO_Init(GPIO_C, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = PIN_D;
    HAL_GPIO_Init(GPIO_D, &GPIO_InitStruct);
	
	HAL_GPIO_WritePin(GPIO_CMD, PIN_CMD, GPIO_PIN_SET);

}
void motor0_signal(GPIO_PinState A, GPIO_PinState B, GPIO_PinState C, GPIO_PinState D, uint16_t delay)
{
		HAL_GPIO_WritePin(GPIO_D, PIN_D, D);
		HAL_GPIO_WritePin(GPIO_C, PIN_C, A);
		HAL_GPIO_WritePin(GPIO_B, PIN_B, B);
		HAL_GPIO_WritePin(GPIO_A, PIN_A, C);
	
	
	HAL_Delay(delay);
	
}
void motor0_stop(void)
{
//	__HAL_RCC_GPIOD_CLK_DISABLE();
//	__HAL_RCC_GPIOE_CLK_DISABLE();
//	__HAL_RCC_GPIOF_CLK_DISABLE();
//	__HAL_RCC_GPIOA_CLK_DISABLE();
	HAL_GPIO_WritePin(GPIO_CMD, PIN_CMD, GPIO_PIN_RESET);
	status = 0;
}




static motor_p g_Motor_p = NULL;

void Motor_Register(motor_p motorp)
{
	motorp->ptNEXT = g_Motor_p;
	
	g_Motor_p = motorp;

}

motor_p Motor_Select(char * name)
{
	motor_p Motor_p = g_Motor_p;
	while(Motor_p)
	{
		if(strcmp(Motor_p->name,name)==0)
		{
			return Motor_p;
			
		}
		Motor_p = Motor_p->ptNEXT;
	}

	return NULL;
}

uint16_t buff[4] = {0,1,1,1};
struct motor Motor0={
	
	.name = "motor0",
	.angle_n =0,
	.angle_pre=0,
	.speed = 10,
	.buff_pre=buff,
	.Motor_Init = motor0_init,
	.Motor_Run = motor0_signal,	
	.Motor_Stop = motor0_stop,
	
	
	
	
};


void ALL_Motor(void)
{
	Motor_Register(&Motor0);
	status = 1;
	
}

void motor_Turnback(motor_p motorp, int signal_time, int* status)
{
		static int i = 0;
	static int x;
	
	if(status == 0)
	{
		return;
	}
	
	motorp->angle_pre = motorp->angle_n;
	
	
		motorp->Motor_Run((GPIO_PinState)motorp->buff_pre[3], 
						(GPIO_PinState)motorp->buff_pre[2], 
						(GPIO_PinState)motorp->buff_pre[1], 
						(GPIO_PinState)motorp->buff_pre[0], signal_time);
	

	i++;	
	if(i==8)
	{
		i=0;
	}

	switch (i)
	{
		case 0: x=7;break;
		case 1: x=3;break;	
		case 2: x=11;break;
		case 3: x=9;break;
		case 4:x=13;break;
		case 5: x=12;break;	
		case 6: x=14;break;
		case 7: x=6;break;
		
	}
	* status =i;
	
	if(motorp->angle_n == 0)
	{
		motorp->angle_n = 4096;
	}
	motorp->angle_n =  motorp->angle_n --;
	
	
	motorp->buff_pre[3] = (0x01&x);
	motorp->buff_pre[2] = (0x01&(x>>1));	
	motorp->buff_pre[1] = (0x01&(x>>2));	
	motorp->buff_pre[0] = (0x01&(x>>3));	
	
	
	
}

void motor_Turn(motor_p motorp, int signal_time, int* status)
{
	static int i = 0;
	static int x;
	
	if(status == 0)
	{
		return;
	}
	
	motorp->angle_pre = motorp->angle_n;
	
	
		motorp->Motor_Run((GPIO_PinState)motorp->buff_pre[0], 
						(GPIO_PinState)motorp->buff_pre[1], 
						(GPIO_PinState)motorp->buff_pre[2], 
						(GPIO_PinState)motorp->buff_pre[3], signal_time);
	i++;	
	if(i==8)
	{
		i=0;
	}

	switch (i)
	{
		case 0: x=7;break;
		case 1: x=3;break;	
		case 2: x=11;break;
		case 3: x=9;break;
		case 4:x=13;break;
		case 5: x=12;break;	
		case 6: x=14;break;
		case 7: x=6;break;
		
	}
	* status =i;
	motorp->angle_n =  motorp->angle_n ++;
	if(motorp->angle_n == 4096)
	{
		motorp->angle_n = 0;
	}
	
	motorp->buff_pre[3] = (0x01&x);
	motorp->buff_pre[2] = (0x01&(x>>1));	
	motorp->buff_pre[1] = (0x01&(x>>2));	
	motorp->buff_pre[0] = (0x01&(x>>3));	
	

						//		0  1   1  1 
//	motorp->Motor_Run((GPIO_PinState)0, 0, 1, 1, 50);
//	motorp->Motor_Run((GPIO_PinState)1, 0, 1, 1, 50);	
//	motorp->Motor_Run((GPIO_PinState)1, 0, 0, 1, 50);	
//	motorp->Motor_Run((GPIO_PinState)1, 1, 0, 1, 50);	
//	motorp->Motor_Run((GPIO_PinState)1, 1, 0, 0, 50);	
//	motorp->Motor_Run((GPIO_PinState)1, 1, 1, 0, 50);	
//	motorp->Motor_Run((GPIO_PinState)0, (GPIO_PinState)1, 1, 0, 50);
}




